package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.TypeReference;
import com.yjc.drygoods.ware.entity.InventoryAlertsEntity;
import com.yjc.drygoods.ware.feign.ProductFeignService;
import com.yjc.drygoods.ware.service.InventoryAlertsService;
import com.yjc.drygoods.ware.to.SpuInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 库存预警表
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-10-26 15:02:13
 */
@RestController
@RequestMapping("ware/inventoryalerts")
public class InventoryAlertsController {
    @Autowired
    private InventoryAlertsService inventoryAlertsService;

    @Autowired
    ProductFeignService productFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = inventoryAlertsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        InventoryAlertsEntity inventoryAlerts = inventoryAlertsService.getById(id);

        return R.ok().put("inventoryAlerts", inventoryAlerts);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody InventoryAlertsEntity inventoryAlerts) {
        R r = productFeignService.spuInfo(inventoryAlerts.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        SpuInfoTo data = r.getData("spuInfo", new TypeReference<SpuInfoTo>() {
        });
        inventoryAlerts.setSpuName(data.getSpuName());
        inventoryAlerts.setWarningTime(new Date());
        inventoryAlertsService.save(inventoryAlerts);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody InventoryAlertsEntity inventoryAlerts) {
        R r = productFeignService.spuInfo(inventoryAlerts.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        SpuInfoTo data = r.getData("spuInfo", new TypeReference<SpuInfoTo>() {
        });
        inventoryAlerts.setSpuName(data.getSpuName());
        inventoryAlerts.setProcessTime(new Date());
        inventoryAlertsService.updateById(inventoryAlerts);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        inventoryAlertsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
